/*
 * Copyright (C) 2004-2010  Autodesk, Inc.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of version 2.1 of the GNU Lesser
 * General Public License as published by the Free Software Foundation.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifndef FDOCMSCONNECTION_H
#define FDOCMSCONNECTION_H

#ifdef _WIN32
#pragma once
#endif // _WIN32

#include <Fdo/Schema/FeatureSchemaCollection.h>
#include <CMS/Override/FdoCMSOvPhysicalSchemaMapping.h>
#include "FdoCMSConnectionImp.h"

// Class Declarations
class FdoCMSConnection : public FdoIConnection
{
public:
    // <summary>Default Contructor.</summary>
    // <returns>Returns nothing</returns> 
    FdoCMSConnection (void);

protected:
    // <summary>Virtual Destructor.</summary>
    // <returns>Returns nothing</returns> 
    virtual ~FdoCMSConnection(void);

    // <summary>Dispose this object.</summary>
    // <returns>Returns nothing</returns> 
    void Dispose (void);

public:
    /// <summary>Gets all the supported map types from Google maps server.</summary>
    /// <returns>Returns string collection of all supported map types.</returns> 
    FdoStringCollection* GetSupportedStyles();

    /// <summary>Gets all the supported image formats from Google maps server.</summary>
    /// <returns>Returns string collection of all supported image formats.</returns> 
    FdoStringCollection* GetSupportedImageFormats();

    /// <summary>Gets an FdoIConnectionCapabilities interface describing the capabilities
    /// of the connection.</summary>
    /// <returns>Returns the connection capabilities</returns> 
    virtual FdoIConnectionCapabilities* GetConnectionCapabilities ();

    /// <summary>Gets an FdoISchemaCapabilities interface describing the provider's support for the feature schema.</summary>
    /// <returns>Returns schema capabilities</returns> 
    virtual FdoISchemaCapabilities* GetSchemaCapabilities ();

    /// <summary>Gets an FdoICommandCapabilities interface describing the commands a provider supports.</summary>
    /// <returns>Returns the command capabilities</returns> 
    virtual FdoICommandCapabilities* GetCommandCapabilities ();

    /// <summary>Gets an FdoIFilterCapabilities interface describing the provider's support for filters.</summary>
    /// <returns>Returns the filter capabilities</returns> 
    virtual FdoIFilterCapabilities* GetFilterCapabilities ();

    /// <summary>Gets an FdoIExpressionCapabilities interface describing the provider's support for expressions.</summary>
    /// <returns>Returns the expression capabilities</returns> 
    virtual FdoIExpressionCapabilities* GetExpressionCapabilities ();

    /// <summary>Gets an FdoIRasterCapabilities interface describing the provider's support for raster images.</summary>
    /// <returns>Returns the raster capabilities</returns> 
    virtual FdoIRasterCapabilities* GetRasterCapabilities ();

    /// <summary>Gets an FdoITopologyCapabilities interface describing the provider's support for topology.</summary>
    /// <returns>Returns the topology capabilities</returns> 
    virtual FdoITopologyCapabilities* GetTopologyCapabilities ();

    /// <summary>Gets an FdoIGeometryCapabilities interface describing the provider's support for geometry.</summary>
    /// <returns>Returns the geometry capabilities</returns> 
    virtual FdoIGeometryCapabilities* GetGeometryCapabilities();

    /// <summary>Gets the connection string used to open a DataStore.</summary>
    /// <returns>Returns the connection string</returns> 
    virtual FdoString* GetConnectionString ();

    /// <summary>Sets the connection string used to open a DataStore. SetConnectionString can only be set while the
    /// connection is closed.</summary>
    /// <param name="value">Input the connection string</param> 
    /// <returns>Returns nothing</returns> 
    virtual void SetConnectionString (FdoString* value);

    /// <summary>Gets an FdoIConnectionInfo interface that can be used to interrogate and set connection properties.</summary>
    /// <returns>Returns the connection info</returns> 
    virtual FdoIConnectionInfo* GetConnectionInfo ();

    /// <summary>Gets the current state of the connection.</summary>
    /// <returns>Returns the current state of the connection</returns> 
    virtual FdoConnectionState GetConnectionState ();

    /// <summary>Gets the number of milliseconds to wait while trying to establish a
    /// connection before terminating the attempt and generating an error. If
    /// the provider does not support the timeout capability 0 will be returned</summary>
    /// <returns>Returns the time to wait (in milliseconds)</returns> 
    virtual FdoInt32 GetConnectionTimeout ();

    /// <summary>Sets the number of milliseconds to wait while trying to establish a
    /// connection before terminating the attempt and generating an error. If
    /// the provider does not support the timeout capability then attempting to
    /// set a timeout will result in an exception</summary>
    /// <param name="value">Input the time to wait (in milliseconds)</param> 
    /// <returns>Returns nothing</returns> 
    virtual void SetConnectionTimeout (FdoInt32 value);

    /// <summary>Opens a feature connection with the settings specified by the
    /// ConnectionString attribute of the provider-specific feature connection
    /// object.</summary>
    /// <returns>Returns nothing</returns> 
    virtual FdoConnectionState Open ();

    /// <summary>Closes the connection to the DataStore</summary>
    /// <returns>Returns nothing</returns> 
    virtual void Close ();

    /// <summary>Begins a transaction and returns an object that realizes
    /// FdoITransaction.</summary>
    /// <returns>Returns the transaction</returns> 
    virtual FdoITransaction* BeginTransaction ();

    /// <summary>Creates and returns the specified type of command object associated with
    /// the connection.</summary>
    /// <param name="commandType">Input the command type to be created</param> 
    /// <returns>Returns the command</returns> 
    virtual FdoICommand* CreateCommand (FdoInt32 commandType);

    /// <summary>Factory function that creates an empty Schema Override set specific
    /// to this FDO Provider.</summary>
    /// <returns>Returns FdoPhysicalSchemaMapping</returns> 
    virtual FdoPhysicalSchemaMapping* CreateSchemaMapping();

    /// <summary>Sets the XML configuration stream used to configure the Data Store. 
    /// SetConfiguration can only be called while the connection is closed.</summary>
    /// <param name="configStream">Input the XML configuration stream</param> 
    /// <returns>Returns nothing</returns> 
    void SetConfiguration(FdoIoStream* configStream);

    /// \brief
    /// Forces the writes of any cached data to the targed datastore.
    /// 
    /// \return
    /// Returns nothing
    /// 
    virtual void Flush() {}

public:
    /// <summary>Gets the name of the Google Map server set through the connection string.</summary>
    /// <returns>Returns the name of the CMS Server</returns> 
    FdoString* GetCMSServerName();

    /// <summary>Gets the Google Map server Metadata.</summary>
    /// <returns>Returns the ServiceMetadata interface.</returns> 
//    FdoCMSServiceMetadata* GetCMSServiceMetadata();

    /// <summary>Determines if the connection has been configured from a configuration file.</summary>
    /// <returns>Returns the connections configured state.</returns> 
    FdoBoolean IsConfigured();

    /// <summary>Gets the physical schema mappings.</summary>
    /// <returns>Returns the physical schema mappins. Returns NULL if the connection is not configured.</returns>
    FdoPhysicalSchemaMappingCollection* GetSchemaMappings (FdoBoolean bGenerateDefault = false);

    /// <summary>Get the feature schemas of the connection.</summary>
    /// <returns>Returns the feature shcema collection.</returns>
    FdoFeatureSchemaCollection* GetSchemas ();

    ///// <summary>Get the dictionary that contains the mappings between FDO applicable class names and CMS layer names.</summary>
    ///// <returns>Returns the layer name dictionary.</returns>
    //FdoDictionary* GetNamedLayerMappings ();

    /// <summary>Search the class and it's parents for its raster property definition.</summary>
    /// <returns>Return the raster definition for the specified class. If not found, return NULL.</returns>
    //FdoRasterPropertyDefinition* FindRasterProperty (FdoClassDefinition* featClass);

    FdoString* GetMapType();
    FdoString* GetActiveSpatialContext ();
    void SetMapType(FdoString* name);

protected:
    /**
     * The connection string containing details about how to connect to Google map server.
     */
    FdoStringP mConnectionString;

    /**
     * Connection information object.
     */
    FdoPtr<FdoIConnectionInfo> mConnectionInfo;

    /**
     * Google Map Server
     */
    FdoStringP mServer;

    /**
     * Connection state
     */
    FdoConnectionState mState;

    /**
    * Google Map Server Capabilities
    **/
//    FdoCMSServiceMetadataP mCMSServiceMetadata; 

    /**
    * Configuration file elements (logical schema + schema mappings):
    **/
    FdoFeatureSchemasP mConfigLogicalSchemas;
    FdoSchemaMappingsP mConfigSchemaMappings;

    /**
     * Flag true when configuration file is specified.
     */
    FdoBoolean mConfigured;

    /**
     * Cached feature schemas
     */
    FdoFeatureSchemasP mSchemas;


    /**
     * Implementer for different commercial services
     */
    FdoCMSConnectionImp* mImp;

private:

    // build up the feature schemas
    void _buildUpDefaultSchemaMappings ();

	// helper function to create a feature class from a CMS layer and 
	// add the class into the collection
    void _addFeatureClass (FdoClassCollection* featClasses, FdoClassDefinition* parent);

	// add properties "FeatId" and "Raster" to the class. Since "FeatId"
	// is identifier of all CMS layer classes, it will be added to
	// m_baseProperties and m_identityProperties; "Raster" is also read-only
	// as "FeatId", so it will be added to m_baseProperties as well.
	void _setProperties (FdoFeatureClass* featClass);

    // Creates a set of default physical schema mappings for the current connection
    void _buildUpDefaultPhysicalSchemaMappings();
};

#endif

